﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
using System.Windows.Interop;
using System.Windows.Media;
using System.Xml.Linq;

namespace PaiBanDaPing
{
    public class ScreenListCtrlViewModel : ObservableObject
    {
        private ObservableCollection<CustomScreenModel> _screenList = new ObservableCollection<CustomScreenModel>();
        XDocument doc = new XDocument();
        string autoupdateFile = "autoupdate.xml";
        UpdateXMLModel updateXML = new UpdateXMLModel();
        LocalXMLModel localXML = new LocalXMLModel();
        List<string> _urls = new List<string>();

        public ObservableCollection<CustomScreenModel> ScreenList
        {
            get
            {
                return _screenList;
            }

            set
            {
                _screenList = value;
                RaisePropertyChanged("ScreenList");
            }
        }

        public ScreenListCtrlViewModel()
        {
            InitXML();
        }

        private void InitXML()
        {
            if (File.Exists(autoupdateFile))
            {
                try
                {
                    doc = XDocument.Load(autoupdateFile);
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show(ex.Message);
                }
            }
            else
                System.Windows.MessageBox.Show(autoupdateFile + "不存在");
        }

        private void DisposeImageSource()
        {
            foreach (var item in ScreenList)
            {
                item.BitMap = null;
                item.DisplayScreen = null;
            }
        }

        public void GetScreens()
        {
            try
            {
                //DisposeImageSource();
                //ScreenList.Clear();
                int primaryWidth = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size.Width - 100;
                int primaryHeight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size.Height;
                double bili = primaryHeight * 1.0 / primaryWidth;
                int i = 0;
                if (doc.Root.Element("DaPing") == null)
                {
                    doc.Root.Add(new XElement("DaPing"));
                }
                IEnumerable<XElement> urls = doc.Root.Element("DaPing").Elements("Url");

                foreach (var item in urls)
                {
                    _urls.Add(item.Value);
                }
                List<CustomScreenModel> tempList = new List<CustomScreenModel>();
                foreach (var screen in System.Windows.Forms.Screen.AllScreens)
                {
                    bool isHave = false;
                    if (!screen.Primary)
                    {
                        foreach (var item in ScreenList)
                        {
                            if (item.DisplayScreen.DeviceName.Equals(screen.DeviceName))
                            {
                                tempList.Add(item);
                                isHave = true;
                                break;
                            }
                        }
                        if (isHave)
                        {
                            CustomScreenModel model = ScreenList.First(O => O.DisplayScreen.DeviceName.Equals(screen.DeviceName));
                            model.BitMap= ImageHelper.ChangeBitmapToImageSource(ImageHelper.GetScreenBitmap(screen, (int)model.DisplayWidth, (int)model.DisplayHeight));
                        }
                        else
                        {
                            CustomScreenModel cScreen = new CustomScreenModel();
                            cScreen.DisplayWidth = (primaryWidth - 100) * 1.0 / 2;
                            cScreen.DisplayHeight = cScreen.DisplayWidth * bili;
                            cScreen.DisplayScreen = screen;
                            cScreen.index = i;
                            if (i < urls.Count())
                            {
                                cScreen.Url = _urls[i];
                            }
                            else
                            {
                                doc.Root.Element("DaPing").Add(new XElement("Url"));
                                doc.Save(autoupdateFile);
                            }
                            cScreen.BitMap = ImageHelper.ChangeBitmapToImageSource(ImageHelper.GetScreenBitmap(screen, (int)cScreen.DisplayWidth, (int)cScreen.DisplayHeight));
                            ScreenList.Add(cScreen);
                            tempList.Add(cScreen);
                            i++;
                        }
                    }
                }
                CustomScreenModel[] keys= ScreenList.Except(tempList).ToArray();
                foreach(var key in keys)
                {
                    key.BitMap = null;
                    key.DisplayScreen = null;
                    ScreenList.Remove(key);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
